//
//  SetPwdViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/14.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "SetPwdViewController.h"
#import "UIView+circular.h"
#import "MBProgressHUD+MJ.h"
#import "LoginDataMange.h"

#import "UserModal.h"
#import "config.h"
#import "StringTool.h"

@interface SetPwdViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *pwd1;
@property (weak, nonatomic) IBOutlet UITextField *pwd2;
@property (strong, nonatomic) LoginDataMange *dataMange;

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation SetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
    
    
}

-(void)initView
{
    [self.titleView setMianTitle:@"设置密码"];
    self.titleView.doneBtn.hidden = YES;
    
    self.pwd1.secureTextEntry=YES;
    self.pwd2.secureTextEntry = YES;
    
    [self.saveBtn viewToCircular];
    self.dataMange = [[LoginDataMange alloc]init];
    
}
#pragma mark 校验密码
-(BOOL)checkPwd
{
    if ([StringTool isBlankString:self.pwd1.text]) {
        [MBProgressHUD showError:@"请输入密码"];
        return NO;
    }else if ([StringTool isBlankString:self.pwd2.text])
    {
        [MBProgressHUD showError:@"请输入确认密码"];
        return NO;
    }else if (self.pwd1.text.length < 6 || self.pwd1.text.length > 20)
    {
        [MBProgressHUD showError:@"设置密码不符合规范"];
        return NO;
    }else if (![self.pwd1.text isEqualToString:self.pwd2.text])
    {
        [MBProgressHUD showError:@"两次密码输入不一致"];
        return NO;
    }
    return YES;

}


#pragma mark 提交密码的请求
-(void)upData
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =  [bself.dataMange resetpWithMobile:bself.phoneStr WithCaptcha:bself.captchaStr WithPassword:bself.pwd1.text WithRePassWord:bself.pwd2.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:bself.view animated:YES];

            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"修改成功"];
                [bself.navigationController popToRootViewControllerAnimated:YES];
            }
        });
    });
    
}

#pragma mark 保存信息
- (IBAction)clickBtn:(id)sender {
    
    if ([self checkPwd]) {
        [self upData];
    }
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.backView];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY - 64;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.backView.frame;
        buf.origin.y = up ? mid : 0;
        [self.backView setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.backView.frame;
    buf.origin.y =  0;
    [self.backView setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}


@end
